package org.jeecg.modules.charge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.entity.basedata.BaseRoom;
import org.jeecg.entity.charge.ChargeStandard;
import org.jeecg.enums.charge.ChargeComputingTypeEnum;
import org.jeecg.enums.charge.ChargeResidentTypeEnum;
import org.jeecg.modules.charge.mapper.ChargeStandardMapper;
import org.jeecg.modules.charge.service.IChargeStandardService;
import org.jeecg.service.IFeignBaseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
 * @Description: 收费标准
 * @Author: jeecg-boot
 * @Date:   2019-07-31
 * @Version: V1.0
 */
@Service
public class ChargeStandardServiceImpl extends ServiceImpl<ChargeStandardMapper, ChargeStandard> implements IChargeStandardService {

    @Autowired
    private ChargeStandardMapper chargeStandardMapper;

    @Autowired
    private IFeignBaseDataService feignBaseDataService;

    public Integer getStandarTenant(String roomId,String feeCode,String tenantRoomId){
        Integer result = chargeStandardMapper.getStandarTenant(roomId,feeCode,tenantRoomId);
        return result;
    }
    public Integer getCountStandardRoom(String roomId,String feeId){
        Integer result = chargeStandardMapper.getCountStandardRoom(roomId,feeId);
        return result;
    }

    public Integer insertList(List<ChargeStandard> standardList){
        Integer result = chargeStandardMapper.insertList(standardList);
        return result;
    }

    public ChargeStandard  getByRoomIdStandar(String roomId,String feeCode){
        return  chargeStandardMapper.getByRoomIdStandar(roomId,feeCode);
    }

    @Override
    public String queryProjectName(String projectId) {
        return chargeStandardMapper.queryProjectName(projectId);
    }

    @Override
    public String queryFloorId(String projectId, String floorName) {
        return chargeStandardMapper.queryFloorId(projectId,floorName);
    }

    @Override
    public String queryRoomId(String floorId, String roomName) {
        return chargeStandardMapper.queryRoomId(floorId,roomName);
    }

    @Override
    public Integer existQueryStandard(String roomId,String feeItemId) {
        return chargeStandardMapper.existQueryStandard(roomId,feeItemId);
    }

    @Override
    public List<ChargeStandard> findPage(Page<ChargeStandard> page, ChargeStandard chargeStandard) {
        return chargeStandardMapper.findPage(page,chargeStandard);
    }

    @Override
    public ChargeStandard getStandardByFeeId(String feeId, String roomId, ChargeResidentTypeEnum residentType) {
        return chargeStandardMapper.getStandardByFeeId(feeId,roomId,residentType.getCode());
    }

    @Override
    public ChargeStandard getStandardByFeeIdAndRoomId(String itemId, String roomId) {
        return chargeStandardMapper.getStandardByFeeIdAndRoomId(itemId,roomId);
    }

    public boolean  updateAll(ChargeStandard chargeStandard){
        boolean ok = false;
        if(null != chargeStandard && chargeStandard.getRoomIdList().size() > 0){
            for(String roomId:chargeStandard.getRoomIdList()){
                BaseRoom room = feignBaseDataService.getByRoomIds(roomId,SsoUtil.getLoginUser().getTenantId());
                ChargeStandard stanDard = chargeStandardMapper.getByRoomIdStandar(roomId,chargeStandard.getItemCode()); // 查询原有的收费标准
                if(null == stanDard){
                    continue;
                }
                if(ChargeComputingTypeEnum.CHARGE_COMPUTING_TYPE_MONTH.getCode() == chargeStandard.getComputingType()){
                    stanDard.setFeeCurrent(new BigDecimal(chargeStandard.getPrice()));
                }else{
                    Integer number = room.getBuildArea().compareTo(new BigDecimal(0));
                    if (room.getBuildArea() != null && number > 0  && chargeStandard.getUnitAccount() != null) {
                        BigDecimal num = new BigDecimal(chargeStandard.getPrice());
                        BigDecimal money1 =  num.multiply(room.getBuildArea());
                        money1 = money1.setScale(chargeStandard.getUnitAccount(), BigDecimal.ROUND_HALF_UP);
                        stanDard.setFeeCurrent(money1);
                    }else{
                        stanDard.setFeeCurrent(BigDecimal.ZERO);
                    }
                }
                stanDard.setRemarks(chargeStandard.getRemarks());
                stanDard.setUnitAccount(chargeStandard.getUnitAccount());
                stanDard.setComputingType(chargeStandard.getComputingType());
                stanDard.setPrice(chargeStandard.getPrice());
                stanDard.setUpdateBy(SsoUtil.getLoginUser().getId());
                stanDard.setUpdateTime(new Date());
                ok = updateById(stanDard);
            }
        }
        return ok;
    }
    public Integer deleteStandard(ChargeStandard chargeStandard){
        return chargeStandardMapper.deleteStandard(chargeStandard);
    }
}
